/**
 * 
 */
package com.mall.untils;

import java.text.DecimalFormat;
import java.util.Random;

/**
 * @author Administrator
 *
 */
public class MathUtil {

	
	/** 随机数对象 */
	private static final Random random = new Random();
	/** 数字与字母字典 */
	private static final char[] LETTER_AND_DIGIT = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	/** 数字与字母字典长度 */
	private static final int LETTER_AND_DIGIT_LENGTH = LETTER_AND_DIGIT.length;
	
	

	/**
	 * 取得随机数
	 * @param len 随机数长度
	 * @return 生成的随机数
	 */
	public static String getRandomString(final int len) {
		if (len < 1) return "";
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			sb.append(LETTER_AND_DIGIT[random.nextInt(LETTER_AND_DIGIT_LENGTH)]);
		}
		return sb.toString();
	}


	
	/**
	 * 随机的四位数字
	 * @param len
	 * @return
	 */
	public static String getRandomNum(final int len){
		if (len < 1) return "";
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
		
	}
	
	/**
	 * 价格格式小数点后2位
	 * @param f
	 * @return
	 */
	public static String getFormatPrice(Float f){
		DecimalFormat dformat = new DecimalFormat("#0.00");
		return dformat.format(f);
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		DecimalFormat dformat = new DecimalFormat("#.00");
		String s = dformat.format(320.5);
		System.out.println(s);
		
	}

}
